/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package telas;

import classes.Prova;
import classes.Xml;
import java.util.Vector;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

/**
 *
 * @author Tiago
 */
public class TelaGerarProvas extends javax.swing.JFrame {

    /**
     * Creates new form TelaGerarProvas
     */
    private Vector<String> materias = new Vector<>();
    Xml xmlMateria = new Xml("materia.xml");
    Prova prova;

    public TelaGerarProvas() {
        initComponents();
        this.setLocationRelativeTo(null);
        Document doc = xmlMateria.carregaXml();

        Element elem = doc.getDocumentElement();
        NodeList nl = elem.getElementsByTagName("materia");
        for (int i = 0; i < nl.getLength(); i++) {
            Element tag = (Element) nl.item(i);
            materias.add(tag.getAttribute("descricao"));
        }
        ComboBoxModel boxModel = new DefaultComboBoxModel(materias);
        cmbMateria.setModel(boxModel);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblTitulo = new javax.swing.JLabel();
        lblNivel = new javax.swing.JLabel();
        lblMateria = new javax.swing.JLabel();
        lblNumeroDePerguntas = new javax.swing.JLabel();
        lblProfessor = new javax.swing.JLabel();
        txtProfessor = new javax.swing.JTextField();
        btnGerarProva = new javax.swing.JButton();
        txtNumeroDePerguntas = new javax.swing.JTextField();
        cmbNivel = new javax.swing.JComboBox();
        cmbMateria = new javax.swing.JComboBox();
        lblData = new javax.swing.JLabel();
        btnGerarGabarito = new javax.swing.JButton();
        txtData = new com.toedter.calendar.JDateChooser();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Gerar Provas");
        setResizable(false);

        lblTitulo.setText("Gerar Provas ");

        lblNivel.setLabelFor(cmbNivel);
        lblNivel.setText("Nível:");

        lblMateria.setLabelFor(cmbMateria);
        lblMateria.setText("Matéria:");

        lblNumeroDePerguntas.setLabelFor(txtNumeroDePerguntas);
        lblNumeroDePerguntas.setText("Nº de Perguntas:");

        lblProfessor.setLabelFor(txtProfessor);
        lblProfessor.setText("Professor:");

        btnGerarProva.setText("Gerar Prova");
        btnGerarProva.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                gerarProva(evt);
            }
        });

        cmbNivel.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Fácil", "Médio", "Difícil" }));

        lblData.setText("Data");

        btnGerarGabarito.setText("Gerar Gabarito");
        btnGerarGabarito.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGerarGabaritogerarProva(evt);
            }
        });

        txtData.setDateFormatString("dd/MM/yyyy");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(33, 33, 33)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(lblNumeroDePerguntas)
                                .addGap(18, 18, 18)
                                .addComponent(txtNumeroDePerguntas, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(lblProfessor, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtProfessor, javax.swing.GroupLayout.DEFAULT_SIZE, 189, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(lblNivel, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(cmbNivel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(33, 33, 33)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(lblTitulo, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addGroup(layout.createSequentialGroup()
                                                .addComponent(lblMateria)
                                                .addGap(18, 18, 18)
                                                .addComponent(cmbMateria, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(lblData)
                                        .addGap(33, 33, 33)
                                        .addComponent(txtData, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addGap(0, 0, Short.MAX_VALUE)))
                        .addContainerGap(19, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnGerarProva, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btnGerarGabarito, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(87, 87, 87))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblTitulo)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblNivel, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cmbNivel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblMateria)
                    .addComponent(cmbMateria, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(25, 25, 25)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(lblNumeroDePerguntas)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(txtNumeroDePerguntas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(lblProfessor)
                        .addComponent(txtProfessor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(lblData)
                    .addComponent(txtData, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnGerarProva)
                    .addComponent(btnGerarGabarito))
                .addContainerGap(31, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void gerarProva(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_gerarProva
        int numeroDePerguntas = 0;
        try {
            numeroDePerguntas = Integer.parseInt(txtNumeroDePerguntas.getText().trim());
            if (numeroDePerguntas == 0) {
                mostraMensagem("Insira a Quantidade de Perguntas");
            }
        } catch (NumberFormatException e) {
            mostraMensagem("Insira a Quantidade de Perguntas");
        }

        if (txtProfessor.getText().trim().equals("")) {
            mostraMensagem("Preencha o Nome do Professor(a)");
        } else {

            try {
                prova = new Prova(cmbMateria.getSelectedItem().toString(),
                        cmbNivel.getSelectedItem().toString(), txtProfessor.getText(),
                        numeroDePerguntas, txtData.getDate());
                String provaGerada = prova.gerarProva();
                if (provaGerada.equals("")) {
                    mostraMensagem("Não há perguntas suficientes cadastradas.");
                } else {
                    new TelaVerProvas(provaGerada, false).setVisible(true);
                }
            } catch (Exception e) {
                mostraMensagem("Data inválida");
            }
        }
        
    }//GEN-LAST:event_gerarProva

    private void btnGerarGabaritogerarProva(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGerarGabaritogerarProva
        try {
            new TelaVerProvas(prova.gerarGabarito(), true).setVisible(true);
        } catch (NullPointerException e) {
            mostraMensagem("É necessário gerar a prova antes do gabarito");
        }

    }//GEN-LAST:event_btnGerarGabaritogerarProva

    private void mostraMensagem(String mensagem) {
        JOptionPane.showMessageDialog(this, mensagem, "Gerar Provas", JOptionPane.INFORMATION_MESSAGE);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(TelaGerarProvas.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(TelaGerarProvas.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(TelaGerarProvas.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(TelaGerarProvas.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new TelaGerarProvas().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnGerarGabarito;
    private javax.swing.JButton btnGerarProva;
    private javax.swing.JComboBox cmbMateria;
    private javax.swing.JComboBox cmbNivel;
    private javax.swing.JLabel lblData;
    private javax.swing.JLabel lblMateria;
    private javax.swing.JLabel lblNivel;
    private javax.swing.JLabel lblNumeroDePerguntas;
    private javax.swing.JLabel lblProfessor;
    private javax.swing.JLabel lblTitulo;
    private com.toedter.calendar.JDateChooser txtData;
    private javax.swing.JTextField txtNumeroDePerguntas;
    private javax.swing.JTextField txtProfessor;
    // End of variables declaration//GEN-END:variables
}
